<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.title" placeholder="昵称" style="width: 200px;" class="filter-item"
                  @keyup.enter.native="handleFilter" />
              <el-button v-waves class="filter-item m-l-2" type="primary" icon="el-icon-search" @click="handleFilter">
                  搜索
              </el-button> -->
      <el-button class="filter-item m-l-2" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
        @click="handleCreate">
        添加
      </el-button>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row
      style="width: 100%;">
      <el-table-column label="排序" type="index" width="80" />
      <el-table-column label="名称" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.title }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="路径" min-width="150px" align="center">
          <template slot-scope="{row}">
            <span>{{ row.url }}</span>
          </template>
        </el-table-column> -->
      <el-table-column label="图片" width="200px" align="center">
        <template slot-scope="{ row }">
          <el-image fit="cover" :src="row.image" :preview-src-list="[row.image]" />
        </template>
      </el-table-column>
      <el-table-column label="排序" width="150px" align="center">
        <template slot-scope="{row}">
          <el-button round>{{ row.sort }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近修改" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.update_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" width="110px" align="center">
        <template slot-scope="{row}">
          <el-switch v-model="row.is_show" active-color="#13ce66" @change="isShow(row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status != 'deleted'" size="mini" type="danger" @click="handleDelete(row.id, $index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-if="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px"
        style="width: 600px; margin-left:50px;">
        <el-form-item label="轮播图名称" prop="title">
          <el-input v-model="temp.title" />
        </el-form-item>
        <el-form-item label="路径" prop="image">
          <el-input v-model="temp.image" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="temp.sort" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
  import { getSlide, addSlide, updateSlide, deleteSlide } from '@/api/slide'
  import waves from '@/directive/waves' // waves directive
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  
  export default {
    name: 'ComplexTable',
    components: { Pagination },
    directives: { waves },
  
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
  
        temp: {
          title: '',
          sort: 1,
          image: '',
          url: '',
          is_show: null
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '添加'
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
          title: [{ required: true, message: 'title is required', trigger: 'blur' }],
          image: [{ required: true, message: 'info is required', trigger: 'blur' }]
        },
        downloadLoading: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        getSlide(this.listQuery).then(response => {
          this.list = response.data
          this.total = response.count
          this.listLoading = false
        }).catch(err => {
          this.listLoading = false
        })
      },
      // 搜索触发
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleModifyStatus(row, status) {
        this.$message({
          message: '操作Success',
          type: 'success'
        })
        row.status = status
      },
      // 添加弹窗的数据
      resetTemp() {
        this.temp = {
          title: '',
          sort: 1,
          info: '/'
        }
      },
      // 添加弹窗
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      // 添加数据
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addSlide(this.temp).then(response => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
            }).catch(err => {
              console.log(err)
            })
          }
        })
      },
      // 编辑弹窗
      handleUpdate(row) {
        this.temp = Object.assign({}, row)
        this.fileList = [{ url: row.image }]
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      // 推荐开关触发
      isShow(row) {
        updateSlide({
          id: row.id,
          is_show: row.is_show
        }).then(response => {
          this.dialogFormVisible = false
          this.$notify({
            title: '操作成功',
            message: '是否推荐首页更改成功',
            type: 'success',
            duration: 2000
          })
        }).catch(err => {
          this.dialogFormVisible = false
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            updateSlide(tempData).then(response => {
              const index = this.list.findIndex(v => v.id === this.temp.id)
              this.list.splice(index, 1, this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
            }).catch(err => {
              this.dialogFormVisible = false
            })
          }
        })
      },
      // 删除
      handleDelete(row, index) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteSlide({ delete_ids: row }).then(response => {
            this.$notify({
              title: 'Success',
              message: 'Delete Successfully',
              type: 'success',
              duration: 2000
            })
            this.list.splice(index, 1)
          }).catch(err => {
            console.log(err)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
  
    }
  }
  </script>
  